package sy.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import sy.pageModel.BaseEntity;
import sy.pageModel.DataGrid;
import sy.pageModel.Json;
import sy.pageModel.Member;
import sy.pageModel.MemberAccount;
import sy.service.MemberServiceI;
import sy.util.DesUtils;
import sy.util.StringUtil;
import sy.util.utils.DxUtil;

import java.util.List;

@Controller
@RequestMapping("/memberController")
public class MemberController extends BaseController {
	
	@Autowired
	private MemberServiceI memberService;
	@RequestMapping("/datagrid")
	@ResponseBody
	public DataGrid datagrid(Member member) throws Exception {
		return memberService.datagrid(member);
	}
	
	@RequestMapping("/listAllActiveMain")
	@ResponseBody
	public DataGrid listAllActiveMain(Member member) throws Exception {
		return memberService.listAllActiveMain(member);
	}
	
	@RequestMapping("/recharge")
	@ResponseBody
	public Json recharge(Member member, HttpSession session) throws Exception {
		Json j = new Json();
		try {
			Member m = memberService.recharge(member);
			j.setSuccess(true);
			j.setMsg("充值成功！");
			j.setObj(m);
			
			//add log
			logUserAction(session, "/memberController/recharge", member.toString());
		} catch (Exception e) {
			j.setMsg("充值失败：" + e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(Member member, HttpSession session) {
		Json j = new Json();
		try {
			Member m = memberService.edit(member);
			j.setSuccess(true);
			j.setMsg("修改成功！");
			j.setObj(m);
			
			//add log
			logUserAction(session, "/memberController/edit", member.toString());
		} catch (Exception e) {
			j.setMsg("修改失败：" + e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/editPay")
	@ResponseBody
	public Json editPay(Member member, HttpSession session) {
		Json j = new Json();
		try {
			Member m = memberService.editPay(member);
			j.setSuccess(true);
			j.setMsg("修改成功！");
			j.setObj(m);
			
			//add log
			logUserAction(session, "/memberController/editPay", getEditPayInfo(member));
		} catch (Exception e) {
			j.setMsg("修改失败：" + e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Json add(Member member, HttpSession session) {
		Json j = new Json();
		try {
			Member m = memberService.add(member);
			j.setSuccess(true);
			j.setMsg("添加成功！");
			j.setObj(m);
			
			//add log
			logUserAction(session, "/memberController/add", m.toString());
			
		} catch (Exception e) {
			j.setMsg("添加失败：" + e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/addFree")
	@ResponseBody
	public Json addFree(Member member, HttpSession session) {
		Json j = new Json();
		try {
			Member m = memberService.addFree(member);
			j.setSuccess(true);
			j.setMsg("添加成功！");
			j.setObj(m);
			
			//add log
			logUserAction(session, "/memberController/addFree", m.toString());
			
		} catch (Exception e) {
			j.setMsg("添加失败：" + e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/waitAuditDatagrid")
	@ResponseBody
	public DataGrid waitAuditDatagrid(Member member) throws Exception {
		return memberService.waitAuditDatagrid(member);
	}
	
	@RequestMapping("/audit")
	@ResponseBody
	public Json audit(Member member, HttpSession session) {
		Json j = new Json();
		try {
			Member m = memberService.audit(member);
			j.setSuccess(true);
			j.setMsg("审核成功！");
			j.setObj(m);
			
			//add log
			logUserAction(session, "/memberController/audit", getAuditInfo(member));
		} catch (Exception e) {
			j.setMsg("审核失败：" + e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/item")
	@ResponseBody
	public Json edit(HttpServletRequest request) {
		Json j = new Json();
		try {
			String cardId = request.getParameter("card_id");
			if(StringUtil.isNullOrEmpty(cardId)) {
				throw new Exception("卡号为空！");
			}
			Member m = memberService.getMemberByCardId(cardId);
			j.setSuccess(true);
			j.setObj(m);
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("获取会员信息失败：" + e.getMessage());
		}
		return j;
	}
	
	private String getAuditInfo(Member member) {
		return "cardId='" + member.getCardId() + "', name='" + member.getName() 
				+ "', mobile='" + member.getMobile() + "', status='" + member.getStatus() + "', statusName='" + member.getStatusName() + "'";
	}
	
	private String getEditPayInfo(Member member) {
		return "cardId='" + member.getCardId() + "', name='" + member.getName() 
				+ "', mobile='" + member.getMobile() + "', memberType='" + member.getMemberType() + "', memberTypeName='" + member.getMemberTypeName()
				+ "', regCharge='" + member.getRegCharge() + "', payType='" + member.getPayType() + "', payTypeName='" + member.getPayTypeName() + "'";
	}
	@RequestMapping("/find_phone.action")
	@ResponseBody
	public Json ajaxAction(String str){
		Json js=new Json();
		js.setObj(memberService.findPhoneList(str));
		return js;
	}
	@RequestMapping("/find_phone1.action")
	@ResponseBody
	public List<BaseEntity> ajaxAction1(String str){

		return memberService.findPhoneList(str);
	}
	@RequestMapping("/transmit_by_radio.action")
	@ResponseBody
	public Json transmitByRadio(BaseEntity base){
		Json js=new Json();
		String result = DxUtil.sendDx(new String[]{base.getId()},base.getName()+"\n");
			if (result.equals("100")){
				System.out.println("发送成功");
				js.setObj("1");
			}
		return js;
	}
	
	@RequestMapping("/memberAccounts")
	@ResponseBody
	public List<MemberAccount> getMemberAccounts() throws Exception {
		return memberService.getMemberAccounts();
	}
}
